package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import org.example.pojo.Likes;
import org.example.pojo.Result;
import org.example.service.LikesService;
import org.example.mapper.LikesMapper;
import org.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Xiri
* @description 针对表【likes】的数据库操作Service实现
* @createDate 2025-07-17 17:55:48
*/
@Service
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes>
    implements LikesService{

	@Autowired
	StringRedisTemplate stringRedisTemplate;
	private static final String NOTE_LIKED_PREFIX = "note:liked:";
	private QueryWrapper qw;

	@Override
	public Boolean LikedByUserId(Integer noteId,Integer uid) {
		//查询是否点过赞
		Double score = stringRedisTemplate.opsForZSet().score(NOTE_LIKED_PREFIX + noteId, uid.toString());
		return score != null;
	}
	
	@Autowired
	UsersService usersService;
	
	/**
	 * 显示点赞前5的人
	 */
	public List<String> likedTop5(Integer noteId)
	{

		//(NOTE_LIKED_PREFIX + noteId, uid.toString());
		Set<String> range = stringRedisTemplate.opsForZSet().range(NOTE_LIKED_PREFIX + noteId, 0, 4);
		List<String> list = null;
		if(range.toArray().length != 0)
		list = this.baseMapper.likedTop5(List.of(range.toArray()));

//		System.out.println(list);
		
		return list;
	}
	
}